Midterm Part 2

#Easier problems

Problem 1: List all of the odd numbers from 1 to 100. you could use the seq() function How could you do this without using the seq() function? Consider using the mod function %%, which evaluates whether or not there is a remainder when dividing one number by another.

for (i in 1:100){
  if(i%%2)
    print(i)
}
[1] 1
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
[1] 13
[1] 15
[1] 17
[1] 19
[1] 21
[1] 23
[1] 25
[1] 27
[1] 29
[1] 31
[1] 33
[1] 35
[1] 37
[1] 39
[1] 41
[1] 43
[1] 45
[1] 47
[1] 49
[1] 51
[1] 53
[1] 55
[1] 57
[1] 59
[1] 61
[1] 63
[1] 65
[1] 67
[1] 69
[1] 71
[1] 73
[1] 75
[1] 77
[1] 79
[1] 81
[1] 83
[1] 85
[1] 87
[1] 89
[1] 91
[1] 93
[1] 95
[1] 97
[1] 99

Problem 2 : Generate 100 random numbers within a specific range runif can do this

runif(100,1,100)
  [1] 33.120829 11.896967 19.415353 50.589755 77.228669 95.622781  3.669065
  [8] 76.852002 60.223599 27.964026 67.960080 19.905128 46.424167 61.918571
 [15] 92.148127 34.197386 97.503413 98.473695 74.130158 61.312863  5.291779
 [22]  4.008336  9.421220  7.582252 43.291974 57.419260 32.246282 25.090425
 [29] 70.237259 30.173052 14.526447 21.574690 58.541599 57.236245 33.095637
 [36]  6.809637 11.677427 13.039047 66.440271 15.659806  1.712580  4.729164
 [43] 78.420404 81.791119 69.453659 15.717039 65.732153 57.535243 61.204469
 [50] 52.919830 59.841390 61.423579 47.937576 15.025014 97.023658 90.946999
 [57]  6.591146 45.635287 14.015059 75.121684 33.768011 95.946949 79.831672
 [64] 87.956409 83.932712 41.167302 77.054032 30.925421 81.893093 21.682476
 [71] 59.232083 40.364756 35.998782 86.572747 65.326694 56.946283 48.053602
 [78]  8.832390 48.715849 58.778630 54.986016 88.088183 81.392389 57.200159
 [85] 68.454320  9.872646  1.913512 72.759080 22.786283 84.941448 86.661339
 [92] 43.678315 10.394851 99.809831 93.434756 91.698799 83.654608  4.847305
 [99]  4.931941 99.281867

Problem 3: Create a variable that stores a 20x20 matrix of random numbers

random_num <-matrix(runif(400, min=3,max=45), nrow = 20, ncol=20)

#Harder problems

Problem 1: FizzBuzz List the numbers from 1 to 100 with the following constraints.

This question can be solved using for loop and if else statements

fizzb <- c(1:100)
for (i in 1:length(fizzb)){
  fizzb[i] <-i
  if(i%%3 == 0 & i%%5== 0){
    fizzb[i] ="FizzBuzz"
  } else if (i%%3 ==0){
    fizzb[i] ="Fizz"
  }
else if (i%%5 ==0 )
  fizzb[i] = "Buzz"
}

Problem 2: Assume that a pair of dice are rolled. Using monte carlo-simulation, compute the probabilities of rolling a 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12, respectively.

dice1 <- replicate(1000,(sample(1:6,1,replace= TRUE)))
dice2 <-replicate(1000,(sample(1:6,1,replace= TRUE)))
sim <- c()
combined <- dice1+dice2
sim <- table(combined)/1000

Problem 3: Multiplication table

Generate a matrix for a multiplication table. For example, the labels for the columns could be the numbers 1 to 10, and the labels for the rows could be the numbers 1 to 10. The contents of each of the cells in the matrix should be correct answer for multiplying the column value by the row value.

multiplication_table <-matrix(NA, nrow=10, ncol=10)
colnames(multiplication_table) <- c("1", "2", "3" ,"4", "5", "6", "7","8","9","10")

rownames(multiplication_table) <- c("1", "2", "3" ,"4", "5", "6", "7","8","9","10")

for(i in 1:10) {
  for(j in 1:10) {
   multiplication_table[i, j] = (i*j)
  }
}

Problem 4:

snakes and ladders. how do you add in a representaion of the board, so that you change which square the player is on depending on whether they land on a ladder or snake.

try doing it for one ladder

so i could do something like if number of roll is ==3 then do this cuz curpos is not interacting with my dice roll

save_rolls <- c()
for(sims in 1:100){
curpos<-0
number_of_rolls<-0

while(curpos < 25){
    curpos <- curpos+sample(c(1,2,3,4,5,6),1)
   if (curpos == 3){
     curpos <-11
   }
    if (curpos == 6){
      curpos <- 17
    }
    if (curpos == 10){
      curpos <- 12
    }
    if (curpos == 14){
      curpos <- 4
    }
    if (curpos ==19){
      curpos <-8
    }
    if(curpos == 22){
      curpos <- 20
    }
    if (curpos ==24){
      curpos <- 16
    }
  number_of_rolls <- number_of_rolls+1
}
save_rolls[sims] <- number_of_rolls
}
mean(save_rolls)
[1] 9.52

the end.